import { createRouter, createWebHashHistory } from "vue-router";

/**
 *@登录组件
 **/
import Login from "@/views/login/Login.vue";
import NotFound from "@/views/404/NotFound.vue";
import PhoneCode from "@/views/login/PhoneCode.vue";
import WeChatLogin from "@/views/login/WeChatLogin.vue";
import ChangePassword from "@/views/login/ChangePassword.vue";
import ChangeUserInfo from "@/views/login/ChangeUserInfo.vue";

import My from '@/views/my/My.vue'
import WeChatCode from '@/views/login/WeChatCode.vue'
import RegistrationTerms from '@/views/login/RegistrationTerms.vue'
import AreaCode from '@/views/login/AreaCode.vue'
import Index from '@/views/cart/index.vue'
import RedPacket from '@/views/Packet/RedPacket.vue'

//曾
import Providehome from "@/views/providehome/Providehome.vue";
import Address from "@/views/address/Address.vue";
import ConfirmOrder from "@/views/confirmOrder/ConfirmOrder.vue";

////////////////////////////////////////////////////////////////////
import GoodsList from "@/views/goodsList/GoodsList.vue";

import Register from "@/views/login/Register.vue";
import PhoneLogin from "@/views/login/PhoneLogin.vue";
////////////////////////////////////////////////////////////////////
/**
 *@首页组件
 **/
import Transition from "@/views/home/Transition.vue";
import Home from "@/views/home/Home.vue";

// import My from '@/views/home/My.vue'
import CoffeeWallet from "@/views/CoffeeWallet/CoffeeWallet.vue";
import ChooseCoffee from "@/views/ChooseCoffee/ChooseCoffee.vue";
import ConfirmInside from "@/views/confirmInside/ConfirmInside.vue";
import Mys from "@/views/home/Mys.vue";
import SaoMa from "@/views/home/SaoMa.vue";
import Help from "@/views/home/Help.vue";
import Accounts from "@/views/home/Accounts.vue";
import Personal from "@/views/home/Personal.vue";
import RedeemTheOffer from "@/views/home/RedeemTheOffer.vue";
import AllOffers from "@/views/home/AllOffers.vue";




// 冯
import MealCode from "@/views/code/MealCode.vue";
import CustomerService from "@/views/online/CustomerService.vue";
import ConfirmOrder_1 from "@/views/pay/ConfirmOrder_1.vue";
import Pay from "@/views/pay/Pay.vue";
import BuyFailed from "@/views/pay/BuyFailed.vue";
import BuySuccess from "@/views/pay/BuySuccess.vue";
import PaySuccess from "@/views/pay/PaySuccess.vue";
import RedpacketMsg from "@/views/redpacketmsg/RedpacketMsg.vue";
import UseRules from "@/views/rules/UseRules.vue";
import Activity from "@/views/activity/Activity.vue";

// 充值咖啡钱包
import TopUpWallet from "@/views/TopUpWallet/TopUpWallet.vue";
import StandTreat from "@/views/standTreat/StandTreat.vue";
import Packet from "../views/Packet/RedPacket.vue";
// order-朱加浩负责
import OrderIndex from "@/views/order/index.vue";
import OrderAllComplete from "@/views/order/all_complete.vue";
import OrderComment from "@/views/order/comment.vue";
import OrderNotComplete from "@/views/order/not_complete.vue";
import OrderNowComplete from "@/views/order/now_complete.vue";
//cart-朱加浩负责部分
import CartCancelOrder from "@/views/cart/cancel_order.vue";
import CartGoPay from "@/views/cart/go_pay.vue";
import CartPayComplete from "@/views/cart/pay_complete.vue";
import CartRemarksOrder from "@/views/cart/remarks_order.vue";
import AddressList from "@/views/addressList/AddressList.vue";
import Myaddress from "@/views/myaddress/Myaddress.vue";

const routes = [
    { path: "/", redirect: "/transition" },

    /**
     *@登录组件
     **/
    { path: "/login", name: "login", component: Login },
    { path: "/phonecode", name: "phone-code", component: PhoneCode },
    { path: "/phonelogin", name: "phone-login", component: PhoneLogin },
    { path: "/wechatlogin", name: "wechat-login", component: WeChatLogin },
    { path: "/wechatcode", name: "wechat-code", component: WeChatCode },
    {
        path: "/changepassword",
        name: "change-password",
        component: ChangePassword,
    },
    {
        path: "/changeuserinfo",
        name: "change-user-info",
        component: ChangeUserInfo,
    },
    {
        path: "/registrationterms",
        name: "registration-terms",
        component: RegistrationTerms,
    },
    { path: "/areacode", name: "area-code", component: AreaCode },
    { path: "/register", name: "register", component: Register },
    { path: "/my", name: "my", component: My },
    { path: "/coffeewallet", name: "coffee-wallet", component: CoffeeWallet },
    { path: "/choosecoffee", name: "choose-coffee", component: ChooseCoffee },
    //商品列表路由
    { path: "/goodsList", name: "goodsList", component: GoodsList },
    //购物车路由
    // {
    //   path: "/cart", name: "cart", redirect: '/cart/index', children: [
    //     { path: 'index', name: 'CartIndex', component: Index },
    //   ]
    // },
    /**
     *@首页
     **/
    { path: "/transition", name: "transition", component: Transition },
    { path: "/home", name: "home", component: Home },
    { path: "/mys", name: "mys", component: Mys },
    { path: "/personal", name: "personal", component: Personal },
    { path: "/saoma", name: "sao-ma", component: SaoMa },
    { path: "/help", name: "help", component: Help },
    { path: "/accounts", name: "accounts", component: Accounts },

    { path: "/goodsList", name: "goodsList", component: GoodsList },
    { path: "/topupwallet", name: "topupwallet", component: TopUpWallet },
    { path: "/standtreat", name: "standtreat", component: StandTreat },
    { path: "/redpacket", name: "redpacket", component: Packet },
    //送货上门 (地址选择)
    { path: '/providehome', name: 'providehome', component: Providehome },
    //添加地址
    { path: '/address', name: 'address', component: Address },
    //订单外送
    { path: '/confirmOrder', name: 'confirmOrder', component: ConfirmOrder },
    //订单自提
    { path: '/confirmInside', name: 'confirmInside', component: ConfirmInside },
    //地址选择
    { path: '/addressList', name: 'addressList', component: AddressList },
    //删除地址 管理地址 我的
    { path: '/myaddress', name: 'myaddress', component: Myaddress },
    // 依次是 取餐码、客服、打烊、确认订单-1、微信支付、购买失败、购买成功、支付成功、发送红包、使用规则、活动描述
    { path: "/mealcode", name: "mealcode", component: MealCode },
    { path: "/online", name: "online", component: CustomerService },
    //合并后出现错误，先注释 周六下午-马龙
    // { path: "/close", name: "close", component: CloseTime },
    { path: "/confirmorder_1", name: "confirmorder_1", component: ConfirmOrder_1 },


    { path: "/pay", name: "pay", component: Pay },
    { path: "/cartPay", name: "cartPay", component: Pay },
    { path: "/buyfailed", name: "buyfailed", component: BuyFailed },
    { path: "/buysuccess", name: "buysuccess", component: BuySuccess },
    { path: "/paysuccess", name: "paysuccess", component: PaySuccess },
    { path: "/redpacketmsg", name: "redpacketmsg", component: RedpacketMsg },
    { path: "/userules", name: "userules", component: UseRules },
    { path: "/activity", name: "activity", component: Activity },
    // 订单路由
    {
        path: '/order', name: 'order', meta: { keepAlive: true }, component: OrderIndex, children: [
            // order-朱加浩负责
            {
                path: "all",
                name: "OrderAllComplete",
                component: OrderAllComplete,
                meta: { keepAlive: true },
            },
            {
                path: "not",
                name: "OrderNotComplete",
                component: OrderNotComplete,
                meta: { keepAlive: true },
            },
            {
                path: "now",
                name: "OrderNowComplete",
                component: OrderNowComplete,
                meta: { keepAlive: true },
            },
        ],
    },
    { path: "/order/comment", name: "OrderComment", component: OrderComment },
    // 购物车路由
    {
        path: '/cart', name: 'cart', redirect: '/cart/index', children: [
            // cart-朱加浩部分负责
            /**  
            *@登录组件
            **/
            { path: "/login", name: "login", component: Login },
            { path: "/phonecode", name: "phone-code", component: PhoneCode },
            { path: "/phonelogin", name: "phone-login", component: PhoneLogin },
            { path: "/wechatlogin", name: "wechat-login", component: WeChatLogin },
            { path: "/wechatcode", name: "wechat-code", component: WeChatCode },
            { path: "/changepassword", name: "change-password", component: ChangePassword },
            { path: "/changeuserinfo", name: "change-user-info", component: ChangeUserInfo },
            { path: "/registrationterms", name: "registration-terms", component: RegistrationTerms },
            { path: "/areacode", name: "area-code", component: AreaCode },
            { path: "/register", name: "register", component: Register },
            { path: "/my", name: 'my', component: My },
            { path: "/coffeewallet", name: 'coffee-wallet', component: CoffeeWallet },
            { path: "/choosecoffee", name: 'choose-coffee', component: ChooseCoffee },
            { path: "/redpacket", name: 'red-packet', component: RedPacket },
            //商品列表路由
            { path: "/goodsList", name: "goodsList", component: GoodsList },
            //购物车路由
            // {
            //   path: "/cart", name: "cart", redirect: '/cart/index', children: [
            //     { path: 'index', name: 'CartIndex', component: Index },
            //   ]
            // },
            /**  
            *@首页
            **/
            { path: "/transition", name: "transition", component: Transition },
            { path: "/home", name: "home", component: Home },
            { path: "/mys", name: "mys", component: Mys },
            { path: "/personal", name: "personal", component: Personal },
            { path: "/saoma", name: "sao-ma", component: SaoMa },

            { path: 'index', name: 'CartIndex', component: Index },
            { path: 'cancelOrder', name: 'CartCancelOrder', component: CartCancelOrder, meta: { keepAlive: true } },
            { path: 'goPay', name: 'CartGoPay', component: CartGoPay, meta: { keepAlive: true } },
            { path: 'payComplete', name: 'CartPayComplete', component: CartPayComplete },
            { path: 'remarksOrder', name: 'CartRemarksOrder', component: CartRemarksOrder },
        ]
    },
    //订单自提
    { path: '/confirmInside', name: 'confirmInside', component: ConfirmInside },


    /**
     * @404组件
     **/
    { path: "/:pathMatch(.*)*", name: "not-found", component: NotFound },
    { path: "/", redirect: "/home" },
    { path: "/home", name: "home", component: Home },
    { path: "/goodsList", name: "goodsList", component: GoodsList },

    { path: "/topupwallet", name: "topupwallet", component: TopUpWallet },
    { path: "/standtreat", name: "standtreat", component: StandTreat },
    { path: "/redpacket", name: "redpacket", component: Packet },
    //送货上门 (地址选择)
    { path: '/providehome', name: 'providehome', component: Providehome },
    //添加地址
    { path: '/address', name: 'address', component: Address },
    //订单外送
    { path: '/confirmOrder', name: 'confirmOrder', component: ConfirmOrder },
    //订单自提
    { path: '/confirmInside', name: 'confirmInside', component: ConfirmInside },
    //地址选择
    { path: '/addressList', name: 'addressList', component: AddressList },
    //管理地址 我的
    { path: '/myaddress', name: 'myaddress', component: Myaddress },
    // 依次是 取餐码、客服、打烊、确认订单-1、微信支付、购买失败、购买成功、支付成功、发送红包、使用规则、活动描述
    { path: "/mealcode", name: "mealcode", component: MealCode },
    { path: "/online", name: "online", component: CustomerService },
    { path: "/confirmorder_1", name: "confirmorder_1", component: ConfirmOrder_1 },
    { path: "/pay", name: "pay", component: Pay },
    { path: "/cartPay", name: "cartPay", component: Pay },
    { path: "/buyfailed", name: "buyfailed", component: BuyFailed },
    { path: "/buysuccess", name: "buysuccess", component: BuySuccess },
    { path: "/paysuccess", name: "paysuccess", component: PaySuccess },
    { path: "/redpacketmsg", name: "redpacketmsg", component: RedpacketMsg },
    { path: "/userules", name: "userules", component: UseRules },
    { path: "/activity", name: "activity", component: Activity },
    // 订单路由
    {
        path: '/order', name: 'order', redirect: '/order/all', component: OrderIndex, children: [
            // order-朱加浩负责
            { path: 'all', name: 'OrderAllComplete', component: OrderAllComplete },
            { path: 'not', name: 'OrderNotComplete', component: OrderNotComplete },
            { path: 'now', name: 'OrderNowComplete', component: OrderNowComplete },
        ]
    },
    { path: '/order/comment', name: 'OrderComment', component: OrderComment },




    /**  
    * @404组件
     **/
    { path: "/:pathMatch(.*)*", name: "not-found", component: NotFound },
    { path: "/", redirect: "/home" },
    { path: "/home", name: "home", component: Home },
    { path: "/goodsList", name: "goodsList", component: GoodsList },

    //送货上门 (地址选择)
    { path: "/providehome", name: "providehome", component: Providehome },
    { path: "/address", name: "address", component: Address },
    { path: "/confirmOrder", name: "confirmOrder", component: ConfirmOrder },
    // 依次是 取餐码、客服、打烊、微信支付、购买失败、购买成功、支付成功、发送红包、使用规则、活动描述
    { path: "/mealcode", name: "mealcode", component: MealCode },
    { path: "/online", name: "online", component: CustomerService },
    { path: "/pay", name: "pay", component: Pay },
    { path: "/buyfailed", name: "buyfailed", component: BuyFailed },
    { path: "/buysuccess", name: "buysuccess", component: BuySuccess },
    { path: "/paysuccess", name: "paysuccess", component: PaySuccess },
    { path: "/redpacketmsg", name: "redpacketmsg", component: RedpacketMsg },
    { path: "/userules", name: "userules", component: UseRules },
    { path: "/activity", name: "activity", component: Activity },
    //兑换优惠All offers
    { path: "/redeemtheoffer", name: "redeem-the-offer", component: RedeemTheOffer },
    { path: "/alloffers", name: "all-offers", component: AllOffers },
    /**
     *@404组件
     **/
    { path: "/:pathMatch(.*)*", name: "not-found", component: NotFound },
    // 依次是 取餐码、客服
    { path: "/mealcode", name: "mealcode", component: MealCode },
    { path: "/online", name: "online", component: CustomerService },
    // 订单路由
    {
        path: "/order",
        name: "order",
        redirect: "/order/all",
        component: OrderIndex,
        children: [
            // order-朱加浩负责
            { path: "all", name: "OrderAllComplete", component: OrderAllComplete },
            { path: "not", name: "OrderNotComplete", component: OrderNotComplete },
            { path: "now", name: "OrderNowComplete", component: OrderNowComplete },
            { path: "comment", name: "OrderComment", component: OrderComment },
        ],
    },
    // 门店自取、门店详情
    // 高
    // 门店自取
    {
        path: "/shop",
        name: "shop",
        component: () => import("@/views/shop/shop.vue"),
    },
    //   门店详情
    {
        path: "/details",
        name: "details",
        component: () => import("@/views/shop/details.vue"),
    },
    //   使用咖啡钱包
    {
        path: "/use",
        name: "use",
        component: () => import("@/views/shop/use.vue"),
    },
    //   选择优惠券
    {
        path: "/select",
        name: "select",
        component: () => import("@/views/shop/select.vue"),
    },
    //   使用优惠券
    {
        path: "/coupon",
        name: "coupon",
        component: () => import("@/views/shop/coupon.vue"),
    },
    {
        path: "/address",
        name: "address",
        component: () => import("@/views/shop/address.vue"),
    }
];
const router = createRouter({
    history: createWebHashHistory(),
    routes,
});
// 路由守卫
// router.beforeEach((to,from,next)=>{
//     if (to.path != '/login') {
//         let token = localStorage.getItem('token')
//         if (token) {
//           if(to.path=='/login') return 
//             next()
//         } else {
//              if(to.path == '/transition'){
//               next()
//              }else if(to.path == '/home'){
//               next()
//              }else if(to.path == '/register'){
//               next()
//              }
//              else if(to.path == '/phonelogin'){
//               next()
//              }
//              else if(to.path == '/mys'){
//               next()
//              }
//              else if(to.path == '/wechatcode'){
//               next()
//              }
//              else if(to.path == '/wechatlogin'){
//               next()
//              }
//              else if(to.path == '/phonecode'){
//               next()
//              }
//              else if(to.path == '/areacode'){
//               next()
//              }
//              else if(to.path == '/help'){
//               next()
//              }
//              else if(to.path == '/saoma'){
//               next()
//              }     else if(to.path == '/accounts'){
//               next()
//              }
//         }

//     } else {
//         // 如果去的页面不是登录 那么需要先判断一下有没有token  有token则表示已经登录过 可以直接放行
//         next()
//     }
// })

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("token");
    if (to.path != "/login") {
        if (token) {
            next();
        } else {
            if (to.path == "/transition") {
                next();
            } else if (to.path == "/home") {
                next();
            } else if (to.path == "/register") {
                next();
            } else if (to.path == "/phonelogin") {
                next();
            } else if (to.path == "/mys") {
                next();
            } else if (to.path == "/wechatcode") {
                next();
            } else if (to.path == "/wechatlogin") {
                next();
            } else if (to.path == "/phonecode") {
                next();
            } else if (to.path == "/areacode") {
                next();
            } else if (to.path == "/help") {
                next();
            } else if (to.path == "/saoma") {
                next();
            } else if (to.path == "/accounts") {
                next();
            }
        }
    } else {
        if (token) {
            return;
        }
        next();
    }
});
export default router;
